package com.example.demo.common.utils.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.utils.BaseResponse;
import com.example.demo.common.utils.HttpUtil;
import com.example.demo.common.utils.RSAHelper;
import com.example.demo.common.utils.demo.common.Config;
import com.example.demo.common.utils.demo.model.OpenApiPostCardInfoRequest;
import com.example.demo.common.utils.demo.model.OpenApiPostSignCheckCodeRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by zhangws on 2019/11/4 17:19.
 * 在线签约
 */
public class OnLineSign {
    static Logger log = LoggerFactory.getLogger(Invoices.class);

    /**
     * 提交签约信息
     */
    public static void sign(OpenApiPostCardInfoRequest openApiPostCardInfoRequest) {
        String url = Config.baseUrl + "/open/api/v1/sign/contract";
        String paramsJson = JSONObject.toJSONString(openApiPostCardInfoRequest);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        if (baseResponse.isSuccess()) {
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)) {
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                    log.info("解密结果:{}", dec);
                } catch (Exception e) {
                    log.error("publicKey 有误");
                }
            }
        }
    }

    /**
     * 验证码确认签约
     *
     * @param openApiPostCardInfoRequest
     */
    public static void submit(OpenApiPostSignCheckCodeRequest openApiPostCardInfoRequest) {
        String url = Config.baseUrl + "/open/api/v1/sign/contract/submit";
        String paramsJson = JSONObject.toJSONString(openApiPostCardInfoRequest);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        if (baseResponse.isSuccess()) {
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)) {
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                    log.info("解密结果:{}", dec);
                } catch (Exception e) {
                    log.error("publicKey 有误");
                }
            }
        }
    }

    /**
     * 查询签约结果
     *
     * @param idCards
     */
    public static void querySignResult(List<String> idCards) {
        String url = Config.baseUrl + "/open/api/v1/sign/result";
        String paramsJson = JSONObject.toJSONString(idCards);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        if (baseResponse.isSuccess()) {
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)) {
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs, Config.publicKey);
                    log.info("解密结果:{}", dec);
                } catch (Exception e) {
                    log.error("publicKey 有误");
                }
            }
        }
    }

    /**
     * 个人信息认证
     *
     * @param openApiPostCardInfoRequest
     */
    public static void personInfoCheck(OpenApiPostCardInfoRequest openApiPostCardInfoRequest) {
        String url = Config.baseUrl + "/open/api/v1/personInfo/check";
        String paramsJson = JSONObject.toJSONString(openApiPostCardInfoRequest);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        if (baseResponse.isSuccess()) {
            log.info("验证成功");
        }else{
            log.info("验证失败{}",baseResponse.getErrorMessage());
        }
    }

    /**
     * 支付宝个人信息认证
     *
     * @param openApiPostCardInfoRequest
     */
    public static void zfbPersonInfoCheck(OpenApiPostCardInfoRequest openApiPostCardInfoRequest) {
        String url = Config.baseUrl + "/open/api/v1/personInfo/zfbCheck";
        String paramsJson = JSONObject.toJSONString(openApiPostCardInfoRequest);
        BaseResponse baseResponse = HttpUtil.post(url, paramsJson, Config.appKey, Config.secretkey, Config.publicKey);
        if (baseResponse.isSuccess()) {
            log.info("验证成功");
        }else{
            log.info("验证失败{}",baseResponse.getErrorMessage());
        }
    }

}
